import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:sp_util/sp_util.dart';
import 'package:syncfusion_flutter_datepicker/datepicker.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class MyInviteLogic extends GetxController with SingleGetTickerProviderMixin {
  final state = MyInviteState();

  @override
  void onInit() {
    super.onInit();
    state.tabController = TabController(length: 3, vsync: this);

    print('*************************');
    print(Get.parameters);
    state.from = Get.parameters['from'] ?? 'invite_koc';
    print('*************************');
  }

  void onReady() {
    super.onReady();
    getData();
    update();
  }

  /// 选择时间段
  ///
  /// 选择时间段
  void onSelectionChanged(DateRangePickerSelectionChangedArgs args) {
    if (args.value is PickerDateRange) {
      state.beginDtme =
          DateFormat('yyyy-MM-dd').format(args.value.startDate).toString();

      state.endDtme = DateFormat('yyyy-MM-dd')
          .format(args.value.endDate ?? args.value.startDate)
          .toString();
    }
    update();
  }

  /// 提交选择时间
  void submitTime() {
    var defaultBegin = DateTime.now().subtract(const Duration(days: 4));
    var defaultEnd = DateTime.now().add(const Duration(days: 3));
    print('******* ${state.beginDtme}');
    state.startTime = DateFormat('yyyyMMdd').format(
        (state.beginDtme == null || state.beginDtme.length < 1)
            ? defaultBegin
            : DateTime.parse(state.beginDtme));
    state.endTime = DateFormat('yyyyMMdd').format(
        (state.endDtme == null || state.endDtme.length < 1)
            ? defaultEnd
            : DateTime.parse(state.endDtme));
    state.beginDtme = state.startTime;
    state.endDtme = state.endTime;
    print(state.beginDtme);
    print(state.endDtme);
    state.selectIndex = 5;
    getData();
    update();
  }

  void getData() {
    // if (state.from == 'invite_koc') {
    //   var params = {
    //     "leader_usr_num_id": SpUtil.getInt('empeNumId'),
    //     "time_type_num_id": state.selectIndex - 1,
    //     'begin_time': state.beginDtme,
    //     'end_time': state.endDtme,
    //     "page_num": 1,
    //     "page_size": 10,
    //   };
    //   print('selectIndex: ${state.selectIndex}   ${state.selectIndex < 5}');
    //   if (state.selectIndex < 5) {
    //     params['begin_time'] = '';
    //     params['end_time'] = '';
    //   }
    //   print('*************************');
    //   print('params: $params');
    //   print('*************************');
    //   Api.inviteKOCList(params).listen((event) {
    //     if (event['distribution_salesperson_infos'] != null) {
    //       state.inviteList = event['distribution_salesperson_infos'];
    //       state.totalCount = event['total_count'];
    //     } else {
    //       state.inviteList = [];
    //     }
    //     update();
    //   });
    // } else {
    var params = {
      "target_usr_num_id": SpUtil.getInt('empeNumId'),
      "time_type_num_id": state.selectIndex - 1,
      'begin_time': state.beginDtme,
      'end_time': state.endDtme,
      "page_num": 1,
      "page_size": 10,
    };
    print('selectIndex: ${state.selectIndex}   $params');
    if (state.selectIndex < 5) {
      params['begin_time'] = '';
      params['end_time'] = '';
    }
    Api.inviteList1(params).listen((event) {
      if (event['member_user_relational_models'] != null) {
        state.inviteList = event['member_user_relational_models'];
        state.totalCount = event['total'];
      } else {
        state.inviteList = [];
      }
      update();
    });
  }
  // }

  void onSelect(int index) {
    state.selectIndex = index;
    state.startTime = '';
    state.endTime = '';
    // 调用接口 刷新listVIew
    getData();
    update();
  }

  void onTabChange(int index) {
    state.index = index;
    update();
  }

  /// 下拉刷新
  void refreshData() {
    state.inviteList = [];
    state.page = 1;
    // if (state.from == 'invite_koc') {
    //   var params = {
    //     "leader_usr_num_id": SpUtil.getInt('empeNumId'),
    //     "time_type_num_id": state.selectIndex - 1,
    //     'begin_time': state.beginDtme,
    //     'end_time': state.endDtme,
    //     "page_num": 1,
    //     "page_size": 10,
    //   };
    //   print('selectIndex: ${state.selectIndex}   ${state.selectIndex < 5}');
    //   if (state.selectIndex < 5) {
    //     params['begin_time'] = '';
    //     params['end_time'] = '';
    //   }
    //   print('*************************');
    //   print('params: $params');
    //   print('*************************');
    //   Api.inviteKOCList(params).listen((event) {
    //     if (event['distribution_salesperson_infos'] != null) {
    //       state.inviteList = event['distribution_salesperson_infos'];
    //       state.totalCount = event['total_count'];
    //       state.controller1.refreshCompleted(resetFooterState: true);
    //     } else {
    //       state.inviteList = [];
    //     }
    //     state.controller1.refreshCompleted(resetFooterState: true);
    //     update();
    //   });
    //   update();
    //   return;
    // } else {
    var params = {
      "target_usr_num_id": SpUtil.getInt('empeNumId'),
      "time_type_num_id": state.selectIndex - 1,
      'begin_time': state.beginDtme,
      'end_time': state.endDtme,
      "page_num": 1,
      "page_size": 10,
    };
    print('selectIndex: ${state.selectIndex}   ${state.selectIndex < 5}');
    if (state.selectIndex < 5) {
      params['begin_time'] = '';
      params['end_time'] = '';
    }
    Api.inviteList1(params).listen((event) {
      if (event['member_user_relational_models'] != null) {
        state.inviteList = event['member_user_relational_models'];
        state.totalCount = event['total'];
        state.controller1.refreshCompleted(resetFooterState: true);
      } else {
        state.inviteList = [];
      }
      state.controller1.refreshCompleted(resetFooterState: true);
      update();
    });
    // }
    update();
  }

  /// 上拉加载更多
  void loadMoreData() {
    state.page = state.page + 1;
    // if (state.from == 'invite_koc') {
    //   var params = {
    //     "leader_usr_num_id": SpUtil.getInt('empeNumId'),
    //     "time_type_num_id": state.selectIndex - 1,
    //     'begin_time': state.beginDtme,
    //     'end_time': state.endDtme,
    //     "page_num": state.page,
    //     "page_size": 10,
    //   };
    //   print('selectIndex: ${state.selectIndex}   ${state.selectIndex < 5}');
    //   if (state.selectIndex < 5) {
    //     params['begin_time'] = '';
    //     params['end_time'] = '';
    //   }
    //   print('*************************');
    //   print('params: $params');
    //   print('*************************');
    //   Api.inviteKOCList(params).listen((event) {
    //     List list = state.inviteList;
    //     if (event['distribution_salesperson_infos'].length > 0) {
    //       list.addAll(event['distribution_salesperson_infos']);
    //       state.totalCount = event['total_count'];
    //       state.inviteList = list;
    //       state.controller1.loadComplete();
    //     } else {
    //       state.controller1.loadNoData();
    //     }
    //     update();
    //   });
    // } else {
    var params = {
      "target_usr_num_id": SpUtil.getInt('empeNumId'),
      "time_type_num_id": state.selectIndex - 1,
      'begin_time': state.beginDtme,
      'end_time': state.endDtme,
      "page_num": state.page,
      "page_size": 10,
    };
    print('selectIndex: ${state.selectIndex}   ${state.selectIndex < 5}');
    if (state.selectIndex < 5) {
      params['begin_time'] = '';
      params['end_time'] = '';
    }
    Api.inviteList1(params).listen((event) {
      List list = state.inviteList;
      if (event['member_user_relational_models'].length > 0) {
        list.addAll(event['member_user_relational_models']);
        state.totalCount = event['total'];
        state.inviteList = list;
        state.controller1.loadComplete();
      } else {
        state.controller1.loadNoData();
      }
      update();
    });
    // }
  }

  void dispose() {
    super.dispose();
    state.controller1.dispose();
  }
}
